<template>
    <div class="User-container content-wrapper">

        <div class="filter-wrapper">
            <el-form ref="filterForm" :model="listQuery" :inline="true">
                <el-form-item prop="name" size="medium">
                    <el-input v-model="listQuery.name" placeholder="应用名称"></el-input>
                </el-form-item>

                <el-form-item prop="status" size="medium">
                    <el-select v-model="listQuery.status" placeholder="状态" clearable style="width: 100px">
                        <el-option
                                v-for="item in statusOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item prop="orderBy" size="medium">
                    <el-select v-model="listQuery.orderBy" placeholder="排序" style="width: 120px">
                        <el-option
                                v-for="item in sortOptions"
                                :key="item.key"
                                :label="item.label"
                                :value="item.key">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-button type="primary" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
                <el-button type="primary" size="medium" icon="el-icon-mars-reset" @click="handleResetFilter">重置</el-button>
                <el-button type="primary" size="medium" icon="el-icon-plus" @click="handleCreate">添加</el-button>
                <el-button type="danger" size="medium" icon="el-icon-delete" @click="handleBatchDelete">删除</el-button>
            </el-form>

        </div>

        <el-table
                :data="list"
                border fit
                highlight-current-row
                v-loading="listLoading"
                element-loading-text="给我一点时间"
                @selection-change="handleSelectionChange">

            <el-table-column
                    type="selection"
                    width="55" align="center">
            </el-table-column>
            <el-table-column type="index" width="55" align="center"></el-table-column>


            <el-table-column label="应用名称" width="300px" align="center">
                <template slot-scope="scope"> {{scope.row.name}} </template>
            </el-table-column>

            <el-table-column label="应用编码" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.code}} </template>
            </el-table-column>

            <el-table-column label="状态" width="80px" align="center">
                <template slot-scope="scope">
                    <el-tag type="success" v-if="scope.row.status === 1">正常</el-tag>
                    <el-tag type="danger" v-else>禁用</el-tag>
                </template>
            </el-table-column>

            <el-table-column label="排序" width="80px" align="center">
                <template slot-scope="scope"> {{scope.row.orders}} </template>
            </el-table-column>

            <el-table-column label="备注" min-width="300px" align="center">
                <template slot-scope="scope"> {{scope.row.remarks}} </template>
            </el-table-column>

            <el-table-column label="创建时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.ctime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="最后修改时间" width="150px" align="center">
                <template slot-scope="scope"> {{scope.row.utime | parseTime('{y}-{m}-{d} {h}:{i}')}} </template>
            </el-table-column>

            <el-table-column label="操作" width="180px" align="center">
                <template slot-scope="scope">
                    <el-button size="small" type="primary" icon="edit" @click="handleUpdate(scope.row)">编辑</el-button>

                    <el-button size="small" type="danger" icon="delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <div v-show="!listLoading" class="pagination-container">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="listQuery.page"
                    :page-sizes="[10, 20, 30, 50]"
                    :page-size="listQuery.rows"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>

        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false" @close="handleDialogClose">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name" style="width: 400px;"></el-input>
                </el-form-item>

                <el-form-item label="编码" prop="code">
                    <el-input v-model="form.code" style="width: 400px;"></el-input>
                </el-form-item>

                <el-form-item label="状态" style="width: 300px;">
                    <el-radio-group v-model="form.status">
                        <el-radio :label="1">正常</el-radio>
                        <el-radio :label="2">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>

                <el-form-item label="排序">
                    <el-input-number v-model="form.orders" :min="1"></el-input-number>
                </el-form-item>

                <el-form-item label="备注" style="width: 520px;">
                    <el-input
                            type="textarea"
                            :autosize="{ minRows: 2, maxRows: 4}"
                            placeholder="请输入内容"
                            v-model="form.remarks">
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button v-if="dialogStatus == 'create'" type="primary" @click="create">提 交</el-button>
                    <el-button v-else="" type="primary" @click="update">提 交</el-button>
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                </el-form-item>
            </el-form>

            <!--<span slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button v-if="dialogStatus == 'create'" type="primary" @click="create">确 定</el-button>
                <el-button v-else="" type="primary" @click="update">确 定</el-button>
            </span>-->
        </el-dialog>
    </div>
</template>

<script>
    import { created, updated, deleted, deletedBatch, selectOne, selectList } from '@/api/upms/app'

    const sortOptions = [{label: '升序', key: 'orders asc'}, {label: '降序', key: 'orders desc'}]
    const statusOptions = [{label: '正常', key: 1}, {label: '锁定', key: 2}]

    export default {
        name: 'App',
        data() {
            return {
                list: [],
                total: null,
                listLoading: false,
                multipleSelection: [],
                listQuery: {
                    page: 1,
                    rows: 10,
                    name: undefined,
                    code: undefined,
                    orderBy: 'orders desc'
                },
                dialogFormVisible: false,
                dialogStatus: '',
                textMap: {
                    update: '编辑',
                    create: '创建'
                },
                statusOptions,
                sortOptions,
                form: {
                    name: '',
                    code: '',
                    status: 1,
                    remarks: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入应用名称', trigger: 'blur' }
                    ],
                    code: [
                        { required: true, message: '请输入唯一标识', trigger: 'blur' }
                    ]
                }
            }
        },
        created() {
            this.getTableData()
        },
        methods: {
            handleFilter() {//条件过滤
                this.listQuery.page = 1
                this.getTableData()
            },
            handleResetFilter() {//重置过滤条件
                this.$refs.filterForm.resetFields()
                this.getTableData()
            },
            handleDialogClose() {//关闭窗口回调函数
                this.resetForm()
            },
            handleSelectionChange(val) {//多选
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                this.listQuery.rows = val
                this.getTableData()
            },
            handleCurrentChange(val) {
                this.listQuery.page = val
                this.getTableData()
            },
            handleCreate() {//添加数据窗口
                this.dialogStatus = 'create'
                this.dialogFormVisible = true
            },
            handleUpdate(row) {//修改数据窗口
                selectOne(row.id).then(response => {
                    this.dialogStatus = 'update'
                    this.dialogFormVisible = true
                    this.form = Object.assign({}, response.data)
                })
            },
            handleDelete(row) {//删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleted(row.id).then(response => {
                        if(response.data.status) {
                            this.$notify({
                                title: '成功', message: response.data.message,
                                type: 'success', duration: 2000
                            })
                            this.getTableData()
                        } else {
                            this.$notify({
                                title: '发生错误', message: response.data.message,
                                type: 'error', duration: 2000
                            })
                        }
                    })
                }).catch(() => {
                    this.$message({ type: 'warning', message: '已取消删除' })
                })
            },
            handleBatchDelete() {//批量删除数据
                this.$confirm('确定要删除？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if(this.multipleSelection.length > 0) {
                        let ids = []
                        for(let item of this.multipleSelection) {
                            ids.push(item.id)
                        }
                        deletedBatch({ids: ids.join(',')}).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                        })

                    } else {
                        this.$notify({
                            title: '提示', message: '请选择需要删除的记录',
                            type: 'warning', duration: 2000
                        })
                    }
                }).catch(() => {
                    this.$message({ type: 'info', message: '已取消删除' })
                })
            },
            create() {//添加数据
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        created(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.handleResetFilter()
                            } else {
                                this.$notify({
                                    title: '操作失败', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })
            },
            update() {//修改数据
                this.$refs['form'].validate((valid) => {
                    if(valid) {
                        updated(this.form).then(response => {
                            if(response.data.status) {
                                this.$notify({
                                    title: '成功', message: response.data.message,
                                    type: 'success', duration: 2000
                                })
                                this.getTableData()
                            } else {
                                this.$notify({
                                    title: '发生错误', message: response.data.message,
                                    type: 'error', duration: 2000
                                })
                            }
                            this.dialogFormVisible = false
                        })
                    }
                })
            },
            getTableData() {
                this.listLoading = true
                selectList(this.listQuery).then(response => {
                    this.list = response.data.list
                    this.total = response.data.total
                    this.listLoading = false
                }).catch(error => {
                    console.log(error)
                    this.listLoading = false
                })
            },
            resetForm() {//重置表单
                this.$refs['form'].resetFields()
                this.form = {
                    name: '',
                    code: '',
                    status: 1,
                    remarks: ''
                }
            }
        }
    }
</script>

<!-- 局部样式 -->
<style rel="stylesheet/scss" lang="scss" scoped>

</style>
